﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Management;
//using System.Windows.Forms;
using MixScript.Appl.Config;
using MixScript.Appl.Security.Crypto;



//using MixScript.Appl.Config;
//using studio.AppLib;
//using studio.server;
//using System.Windows.Forms;
namespace MixScript.Appl.Config
{


    public class cBaseConfig
    {        
        private static string hdserialnumber = "";
        private static bool havelisence;
        private static string msg;
        private static DateTime dtlisence;
        private static string lisence;

        private static string dbhost = "";
        private static string dbname;       
        private static string userdb;
        private static string passdb;
        private static string provider;
        

        //public static Dictionary<string, string> dicAPPSet = new Dictionary<string, string>();

        public string Dbname
        {
            get { return dbname; }
            set { dbname = value; }
        }
        public static DateTime DtTmLisence
        {
            get { return cBaseConfig.dtlisence; }
            set { cBaseConfig.dtlisence = value; }
        }
        public static string Msg
        {
            get { return msg; }
            set { msg = value; }
        }
        public static bool HaveLisence
        {
            get { return cBaseConfig.havelisence; }
            set { cBaseConfig.havelisence = value; }
        }
        public static string HDserialnumber
        {
            get { return cBaseConfig.hdserialnumber; }
            set { cBaseConfig.hdserialnumber = value; }
        }
        public static string Lisence
        {
            get { return cBaseConfig.lisence; }
            set { cBaseConfig.lisence = value; }
        }
        public string DBHost
        {
            get { return dbhost; }
            set { dbhost = value; }
        }
        public string Userdb
        {
            get { return userdb; }
            set { userdb = value; }
        }        
        public string Passdb
        {
            get { return passdb; }
            set { passdb = value; }
        }
        public string Provider
        {
            get { return provider; }
            set { provider = value; }
        }

        public cBaseConfig()
        {
            if (DBHost == string.Empty)
            {
                getHDSerialNumber();
                getAppConfig();
                cheklisence();
            }
        }
       
       

      


        public void getAppConfig()
        {
            try
            {
                DBHost = ConfigurationManager.AppSettings["host"].ToString();
                Dbname = ConfigurationManager.AppSettings["dbname"].ToString();
                Userdb = ConfigurationManager.AppSettings["userdb"].ToString();
                Passdb = ConfigurationManager.AppSettings["passdb"].ToString();
                Provider = ConfigurationManager.AppSettings["provider"].ToString();
                lisence = ConfigurationManager.AppSettings["lisence"].ToString();
            }
            catch (Exception ex)
            {
               throw new Exception(ex.Message);
            }
        }
        private void getHDSerialNumber()
        {
            #region Old Code
            //try
            //{
            //    ManagementObjectSearcher searcher = new ManagementObjectSearcher("select * from Win32_DiskDrivePhysicalMedia");
            //    foreach (ManagementObject share in searcher.Get())
            //    {
            //        //PropertyData pd= share.Properties[
            //       // ManagementObject physicalMedia = new ManagementObject(wmi_HD["Antecedent"].ToString());

            //        hdserialnumber = share.Properties["SerialNumber"].Value.ToString();
            //        continue;
            //    }

            //}
            //catch (Exception ex)
            //{

            //    MessageBox.Show(ex.Message);
            //} 
            #endregion

            try
            {
              //  ManagementObjectSearcher searcher2 = new ManagementObjectSearcher(@"SELECT * FROM Win32_DiskDrivePhysicalMedia");
                //if (searcher2 != null)
                //{
                //    foreach (ManagementObject wmi_HD in searcher2.Get())
                //    {
                //        ManagementObject physicalMedia = new ManagementObject(wmi_HD["Antecedent"].ToString());
                //        // MessageBox.Show(physicalMedia["SerialNumber"].ToString());
                //        return;
                //        //ManagementObject diskDrive = new ManagementObject(wmi_HD["Dependendent"].ToString());
                //        //// get the hardware serial no.
                //        //if (diskDrive["DeviceID"].ToString() == @"\\.\PHYSICALDRIVE0")
                //        //{
                //        //    MessageBox.Show(physicalMedia["SerialNumber"].ToString());
                //        //}
                //    }
                //}
            }
            catch (Exception ex)
            {
              //  throw ex;
            }


        }
        private void cheklisence()
        {
            try
            {
                cAES_Cryptography oCrypt = new cAES_Cryptography();
                if (Lisence.Trim() != string.Empty)
                {
                    string s = oCrypt.decryptString(lisence, cAES_Cryptography.encKeys);
                    string[] ss = s.Split('|');
                    if (ss[2] != hdserialnumber || ss[0] != "SELUTASTUDIO")
                    {
                        msg = "Invalid Lisence";
                        havelisence = false;
                        return;
                    }
                    if (DateTime.Now > (Convert.ToDateTime(ss[1])) && ss[1] != "01011990")
                    {
                        msg = "Lisence Expired";
                        havelisence = false;
                        return;
                    }

                    havelisence = true;
                    return;
                }
                else
                {
                    havelisence = false;
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                msg = "invalid Lisence";
                havelisence = false;
            }
        }
       

        





    }

   

  
}
